package com.blog.cloud.auth.controller;

import com.blog.cloud.api.vo.LoginRequestDto;
import com.blog.cloud.api.vo.LoginResponseVo;
import com.blog.cloud.api.vo.ResultVo;
import com.blog.cloud.auth.service.AuthService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Tag(name = "后台管理认证控制器", description = "认证服务")
public class AuthController {

    private final AuthService authService;

    // 登录
    @PostMapping("/login")
    public ResultVo<LoginResponseVo> login(@RequestBody @Valid LoginRequestDto loginRequestDto) {
        LoginResponseVo authLoginResponseVo = authService.login(loginRequestDto);
        return ResultVo.ok(authLoginResponseVo);
    }

    // 注册
    @PostMapping("/register")
    public ResultVo<String> register(@RequestBody @Valid LoginRequestDto loginRequestDto) {
        return ResultVo.ok(authService.register(loginRequestDto));
    }

    // 退出
    @PostMapping("/logout")
    public ResultVo<String> logout() {
        return ResultVo.ok("退出成功");
    }

    // 刷新 accessToken
    @PostMapping("/refreshToken")
    public ResultVo<String> refreshAccessToken(String refreshToken) {
        String newAccessToken = authService.refreshAccessToken(refreshToken);
        return ResultVo.ok(authService.refreshAccessToken(refreshToken));
    }

    // 发送短信验证码
    @PostMapping("/sms/code")
    public ResultVo<String> sendSmsCode(@RequestBody @Valid LoginRequestDto loginRequestDto) {
        return ResultVo.ok(authService.sendSmsCode(loginRequestDto));
    }

    // 发送图形验证码
    @PostMapping("/captcha")
    public ResultVo<String> sendCaptcha(@RequestBody @Valid LoginRequestDto loginRequestDto) {
        return ResultVo.ok(authService.sendCaptcha(loginRequestDto));
    }

    // 发送邮件验证码
    @PostMapping("/email/code")
    public ResultVo<String> sendEmailCode(@RequestBody @Valid LoginRequestDto loginRequestDto) {
        return ResultVo.ok(authService.sendEmailCode(loginRequestDto));
    }
}
